package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 16/7/7.
 */
public class GetDatiListTask extends CouponHttpBaseTask{
    private int mPageNo;
    private int mSize;
    private String mSubjectNo;
    private String mGrade;
    private String mViewType;

    public GetDatiListTask(int pageNo,int size,String subjectNo,String grade,String viewtype) {
        super();
        mPageNo = pageNo;
        mSize = size;
        mSubjectNo = subjectNo;
        mGrade = grade;
        mViewType = viewtype;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject jsonObject = response.getBody();
        List<DatiInfo> list = new ArrayList<DatiInfo>();
        int currentPage,totalPage,nextPage = 0;
        if (jsonObject != null){
            JSONObject datiList = jsonObject.getJSONObject("data");
            if (datiList != null){
                currentPage = datiList.getIntValue(ProtocolConst.CURRENT_PAGE);
                totalPage = datiList.getIntValue(ProtocolConst.PAGE_COUNT);
                nextPage = currentPage >= totalPage ? 0 :(currentPage+1);
                list = JSON.parseArray(datiList.getString("list"),DatiInfo.class);
            }
        }
        notifyTaskCompleted(nextPage,list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT){
            CouponHttpRequest request = new CouponHttpRequest();
            request.setApiNo(ApiNo.DATI_LIST);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageNo",mPageNo);
            jsonObject.put("size",mSize);
            jsonObject.put("subjectNo",mSubjectNo);
            jsonObject.put("grade",mGrade);
            jsonObject.put("viewType",mViewType);
            request.setData(jsonObject.toString());
            addRequest(request);
        }
    }
}
